package com.myProject.control.user;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TNote;
import com.myProject.serves.Impl.LogSevImpl;
import com.myProject.serves.LogSev;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "UserLogServlet", urlPatterns = "/userLog")
public class UserLogServlet extends HttpServlet {
    LogSev ls = new LogSevImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String med = request.getParameter("method");
        if (med == null || "".equalsIgnoreCase(med)) {

            showAll(request, response);

        }
    }

    private void showAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
        先添加再进行展示
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int pageNo = 1;
        String page = request.getParameter("page");
        if (page != null) {
            pageNo = Integer.parseInt(page);
        }
        int pageSize = Integer.parseInt(request.getParameter("limit"));
        TNote tt = new TNote();
        String date1 = request.getParameter("date1");
        String date2 = request.getParameter("date2");
        if (date1 != null && !"".equalsIgnoreCase(date1)) {
            try {
                tt.setStartTime(sdf.parse(date1));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (date2 != null && !"".equalsIgnoreCase(date2)) {
            try {
                tt.setEndTime(sdf.parse(date2));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.out.println("tt = " + tt);
        PageInfo<TNote> all = ls.getNotePage(pageNo, pageSize, tt);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(all);
        response.getWriter().write(json);

    }

}
